package com.fa.dv.service.impl;

import com.fa.common.utils.SecurityUtils;
import com.fa.dv.domain.Subject;
import com.fa.dv.domain.dto.SubjectDTO;
import com.fa.dv.mapper.SubjectMapper;
import com.fa.dv.service.ISubjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

@Service
@Slf4j
public class SubjectServiceImpl implements ISubjectService {

    @Autowired
    private SubjectMapper subjectMapper;
    @Override
    public List<Subject> selectList(SubjectDTO subjectDTO) {
        return subjectMapper.selectList(subjectDTO);
    }

    @Override
    public void insert(Subject subject) {
        subject.setCreateTime(new Date());
        subject.setUpdateTime(new Date());
        subject.setCreateBy(SecurityUtils.getUsername());
        subject.setUpdateBy(SecurityUtils.getUsername());
        subjectMapper.insert(subject);
    }

    @Override
    public void update(Subject subject) {
        subject.setUpdateTime(new Date());
        subject.setUpdateBy(SecurityUtils.getUsername());
        subjectMapper.update(subject);
    }

    @Override
    public Subject selectById(int subjectId) {
        return subjectMapper.selectById(subjectId);
    }

    @Override
    public void delete(Integer subjectId) {
        subjectMapper.delete(subjectId);
    }

    @Override
    public void deleteList(List<Integer> subjectIds) {
        subjectMapper.deleteList(subjectIds);
    }

    @Override
    public void saveBatch(ArrayList<Subject> cacheList) {
        subjectMapper.insertBatch(cacheList);
    }

    @Override
    public Map<String, ArrayList<? extends Object>> getLogEcharts7Day() {

        // 获取近7天的日期
        ArrayList<String> days = new ArrayList<>();
        ArrayList<Integer> counts = new ArrayList<>();
        LocalDate now = LocalDate.now();
        days.add(now.toString());
        Integer count = subjectMapper.findLoginCountByDay(now.toString());
        counts.add(count);

        for (int i = 1; i < 7; i++) {
            LocalDate day = now.minusDays(i);
            System.out.println("day = " + day);
            count = subjectMapper.findLoginCountByDay(day.toString());
            days.add(day.toString());
            counts.add(count);
            log.info("day = {}, count = {}", day, count);

        }
        Collections.reverse(days);
        Collections.reverse(counts);

        log.info("days = {}", days);
        log.info("counts = {}", counts);

        Map<String, ArrayList<? extends Object>> map = new HashMap<>();
        map.put("xAxisData", days);
        map.put("seriesData", counts);

        return map;
    }
}
